package com.bizfx.component.fields
{
	import flash.events.Event;
	
	import com.bizfx.component.views.IDetailView;
	import com.bizfx.utils.ObjectUtils;
	import com.bizfx.utils.StringUtils;
	
	import mx.containers.HBox;
	import mx.controls.DateField;
	import mx.controls.NumericStepper;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.FlexEvent;
	import mx.events.ValidationResultEvent;
	import mx.utils.UIDUtil;
	import mx.validators.DateValidator;
	
	public class DatePickerField extends DateField implements IField, IValidationField
	{
		private var _validator:DateValidator;
		
		public function DatePickerField() {
			super();
			this.minWidth = 160;
			this.setStyle("backgroundDisabledColor", 0xFFFFFF);
			this.setStyle("disabledOverlayAlpha", 0);
			this.setStyle("disabledColor", 0x000000);
			this.addEventListener(CalendarLayoutChangeEvent.CHANGE, fieldChangeHandler);
			this.yearNavigationEnabled = true;
			this.dayNames=['日','一','二','三','四','五','六'];
			this.monthNames=['一月','二月','三月','四月','五月','六月','七月','八月','九	月','十月','十一月','十二月'];
			this.setStyle("fontSize", 12);
			this.formatString = "YYYY-MM-DD";
			
			this._validator = new DateValidator();
			this._validator.required = false;
			this._validator.requiredFieldError = "必填";
			this._validator.inputFormat = this.formatString;
			this._validator.wrongLengthError = "输入格式错误。正确格式为:";
			this._validator.wrongDayError = "请输入正确的日期。";
			this._validator.wrongMonthError = "请输入1到12之间的数字作为月份。";
			this._validator.wrongYearError = "请输入0到9999之间的数字作为年份。";
		}

		private function fieldChangeHandler(event:CalendarLayoutChangeEvent):void {
			this.data = this.selectedDate;
		}
		
		public function get fieldKey():String
		{
			return this.id;
		}
		
		public function set fieldKey(value:String):void
		{
			id = value;
		}
		
		private var _bindProperty:String;
		
		public function get bindProperty():String
		{
			return _bindProperty;
		}
		
		public function set bindProperty(value:String):void {
			_bindProperty = value;
		}
		
		private var _required:Boolean;
		
		public function get required():Boolean
		{
			return this._validator.required;
		}
		
		public function set required(value:Boolean):void {
			this._validator.required = value;
		}
		
		private var _data:Object;
		
		override public function get data():Object {
			if (_data) {
				return _data;
			} else {
				return new Date(0, 0, 0);
			}
		}
		
		override public function set data(value:Object):void {
			_data = value;
			this.selectedDate = value as Date;
			if (value) {
				if (value.valueOf() == new Date(0, 0, 0).valueOf() || value.valueOf() == new Date(1899, 11, 31).valueOf()) {
					_data = null;
					this.selectedDate = null;
				}
			}
		}
		
//		private var _view:IDetailView;
//		
//		public function get view():IDetailView
//		{
//			return this._view;
//		}
//		
//		public function set view(value:IDetailView):void {
//			if (StringUtils.isNullOrEmpty(this.id)) {
//				this.id = UIDUtil.createUID();
//			}
//			if (this._view !== value) {
//				if (_view) {
//					_view.removeFieldByKey(this.fieldKey);
//				}
//				this._view = value;
//				if (_view) {
//					_view.addField(this);
//				}
//			}
//		}
		
		[Inspectable(category="General", enumeration="true,false", defaultValue="false")]
		[Bindable("readOnlyChanged")]
		public function get readOnly():Boolean {
			return this.enabled;
		}
		
		public function set readOnly(value:Boolean):void {
			if (this.enabled == value) {
				this.enabled = !value;
				this.dispatchEvent(new Event("readOnlyChanged"));
			}
		}
		
		public function validate():void {
			this._validator.source = this;
			this._validator.property = "selectedDate";
			var event:ValidationResultEvent = this._validator.validate(this.text);
			this.dispatchEvent(event);
		}
	}
}